package cn.fsd.controller;

import cn.fsd.domain.Customer;
import cn.fsd.service.CustomerService;
import cn.fsd.query.customer.CustomerQuery;
import cn.fsd.vo.ResultBean;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@ResponseBody
@RequestMapping("/customer")
public class CustomerController {
    final CustomerService customerService;

    @Autowired
    public CustomerController(CustomerService customerService) {
        this.customerService = customerService;
    }

    /**
     * name,resident_id,mobile_phone三个字段条件查询，其中name为模糊查询
     *
     * @param customerQuery 查询请求，包括三个字段，以及pageNum，pageSize
     * @return ResultBean包装的分页对象PageInfo
     */
    @RequestMapping(value = "/find", method = RequestMethod.POST)
    @ApiOperation("客户信息查询")
    public ResultBean<PageInfo<Customer>> find(@RequestBody CustomerQuery customerQuery) {
        return ResultBean.success(customerService.findByConditionByPage(customerQuery));
    }

    /**
     * 返回所有客户的名称集合，用于自动补全
     *
     * @return ResultBean包装的所有客户的名称集合
     */
    @ApiOperation("前端自动补全信息接口")
    @RequestMapping(value = "/completeInfo", method = RequestMethod.GET)
    public ResultBean<List<String>> completeInfo() {
        return ResultBean.success(customerService.completeInfo());
    }

    /**
     * 新增一个客户，其中customer_id由后端生成
     *
     * @param customer customer对象
     */
    @ApiOperation("新建客户")
    @RequestMapping(value = "/addCustomer", method = RequestMethod.POST)
    public ResultBean<String> addCustomer(@RequestBody Customer customer) {
        customerService.addCustomer(customer);
        return ResultBean.success();
    }

    /**
     * 根据id删除一个客户
     *
     * @param id customer_id
     */
    @ApiOperation("删除客户")
    @RequestMapping(value = "/deleteCustomer/{id}", method = RequestMethod.GET)
    public ResultBean<String> deleteCustomer(@PathVariable String id) {
        customerService.deleteCustomer(id);
        return ResultBean.success();

    }

    /**
     * 根据id更新客户的信息，需要传入所有字段（包括id）,未传入的字段会被置为null
     *
     * @param customer 更新后的用户对象
     */
    @ApiOperation("更新客户信息")
    @RequestMapping(value = "/updateCustomer", method = RequestMethod.POST)
    public ResultBean<String> updateCustomer(@RequestBody Customer customer) {
        customerService.updateCustomer(customer);
        return ResultBean.success();
    }

    @RequestMapping(value = "/getCustomerById", method = RequestMethod.GET)
    @ApiOperation("根据id查询客户信息")
    public Customer getCustomerById(@RequestParam("id") String id) {
        return customerService.getCustomer(id);
    }

}
